package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.bean.RoleBean;
import com.woniuxy.caradmin.entity.pojo.Role;
import com.woniuxy.caradmin.entity.pojo.User;
import com.woniuxy.caradmin.entity.vo.RoleVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.RoleService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/selectAllRole")
    @ApiOperation("查询所有的角色")
    public R selectAllRole(){
        List<Role> roles = roleService.selectAllRole();
        if(roles!=null && roles.size()>0){
           return R.ok().data("roles",roles);
        }else{
            throw new CarException(ResultCode.ROLEISNULL,Constaint.ROLEISNULL);
        }
    }
    @GetMapping("/addRole")
    @ApiOperation("增加角色")
    public R addRole(Role role){
        Integer integer = roleService.insertRole(role);
        if(integer>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else {
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @GetMapping("/delectRole")
    @ApiOperation("删除角色")
    public R delectRole(Integer id){
        Integer integer = roleService.delectRole(id);
        if(integer>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else {
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @GetMapping("/updateRole")
    @ApiOperation("修改角色")
    public R updateRole(Role role){
        Integer integer = roleService.updateRole(role);
        if(integer>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else {
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @GetMapping("/roleTable")
    @ApiOperation("查询所有的角色列表")
    public DataGridView roleAll(RoleVo roleVo){
        Page<Object> page = PageHelper.startPage(roleVo.getPage(), roleVo.getLimit());
        List<Role> roles = roleService.selectByName(roleVo);
        return new  DataGridView(page.getTotal(),roles);
    }
    @GetMapping("/roleList")
    @ApiOperation("用户角色管理")
    public DataGridView roleList(Integer uid){
        System.out.println("-----"+uid);
        List<Role> roles = roleService.selectAllRole();
        List<Role> rolesList = roleService.selectUserWithRole(uid);
        List<RoleBean> roleBeans=new ArrayList<>();
        if(roles!=null&&roles.size()>0){
            for (Role role : roles) {
                boolean LAY_CHECKED = false;
                for (Role roleItem : rolesList) {
                    if (role.getId() == roleItem.getId()) {
                        LAY_CHECKED = true;
                    }
                }
                RoleBean roleBean=new RoleBean();
                roleBean.setId(role.getId());
                roleBean.setName(role.getName());
                roleBean.LAY_CHECKED=LAY_CHECKED;
                roleBeans.add(roleBean);
            }
            return new DataGridView((long)roleBeans.size(),roleBeans);
        }
        throw new CarException(ResultCode.ROLEISNULL,Constaint.ROLEISNULL);
    }

    @ApiOperation("批量删除")
    @PostMapping("/deleteAllRolePath")
    public R deleteAllPath(Integer[] ids){
        boolean b = roleService.delectAllPath(ids);
        if (b){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }
        return null;
    }
    @ApiOperation("界面用户删除")
    @PostMapping("/dellRole{id}")
    public R deleUser(@PathVariable("id") String id){
        Integer integer = roleService.delectRole(Integer.parseInt(id));
        if(integer>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else {
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @ApiOperation("界面角色增加")
    @PostMapping("/addUserOn")
    public R addUserOn(HttpServletRequest request){
        String name = request.getParameter("name");
        Role role=new Role();
        role.setName(name);
        Integer integer = roleService.insertRole(role);
        if(integer>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else {
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @PostMapping("/updarole")
    @ApiOperation("页面修改角色")
    public R updarole(HttpServletRequest request){
        String name = request.getParameter("name");
        String id = request.getParameter("roleid");
        System.out.println("aa"+name);
        System.out.println("dd"+id);
        Role role=new Role();
        role.setName(name);
        role.setId(Integer.parseInt(id));
        Integer integer = roleService.updateRole(role);
        if(integer>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else {
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
}

